package org.dean.webapps.projectname.presentation.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 * User: dean
 * Date: 8/29/11
 * Time: 2:49 PM
 * To change this template use File | Settings | File Templates.
 */
public class RequestParameterUtils {
    private static Log log = LogFactory.getLog(RequestParameterUtils.class);

    public static Long getLongId(HttpServletRequest request, String key) {
        Long id = -1L;
        try {
            String idStr = request.getParameter(key);
            if (!StringUtils.isEmpty(idStr)) {
                id = Long.parseLong(idStr);
            }
        } catch (Exception e) {
            id = -1L;
            log.error(e.getMessage(), e);
        }

        return id;

    }

    public static Long getLongId(String idString) {
        Long id = -1L;
        try {
            if (!StringUtils.isEmpty(idString)) {
                id = Long.parseLong(idString);
            }
        } catch (Exception e) {
            id = -1L;
            log.error(e.getMessage(), e);
        }

        return id;

    }

    public static Integer getIntValue(String intString) {
        Integer value = 0;
        try {
            if (!StringUtils.isEmpty(intString)) {
                value = Integer.parseInt(intString);
            }
        } catch (Exception e) {
            value = 0;
            log.error(e.getMessage(), e);
        }

        return value;

    }

    public static Double getDouble(HttpServletRequest request, String key) {
        Double value = 0.0;
        try {
            String valueStr = request.getParameter(key);
            if (!StringUtils.isEmpty(valueStr)) {
                value = Double.parseDouble(valueStr);
            }
        } catch (Exception e) {
            value = 0.0;
            log.error(e.getMessage(), e);
        }

        return value;

    }
}
